<?php

Raise::load('core.RaiseStaticClass');
Raise::load('core.RaiseFile');
Raise::load('core.RaisePath');
Raise::load('core.RaiseException');
Raise::load('core.RaiseCollection');

/**
 * RaiseFileSystem helper class
 * for functionality related to the file system
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.Core.IO
 * @since 1.2
 */
class RaiseFileSystem extends RaiseStaticClass {

    /**
     * Check whether a file exists in the file system
     * @param string $f Path name to the file
     * @return boolean TRUE if the file exists, FALSE otherwise.
     * @static
     */
    public static function fileExist($f){
        return (bool)is_file($f);
    }


    /**
     * Check whether a path directory exists in the file system
     * @param string $f Path name to the directory
     * @return boolean TRUE if the directory exists, FALSE otherwise.
     * @static
     */
    public static function pathExist($p){
        return (bool)is_dir($p);
    }

    /**
     * Create a file and set its content
     * @param string $f Pathname to the file to create and write to.
     * @param string $c (optional) The file content to set.
     * @return RaiseFile The file object that was used to create the file
     * @see RaiseFile
     * @static
     */
    public static function createFile($f, $c = ''){
        $file = new RaiseFile($f);
        $file->createFile();
        if($c){
            $file->setContent($c);
        }
        return $file;
    }

    /**
     * Delete a particular file
     * @param string|RaiseFile $f Pathname to the file to delete
     * @return boolean TRUE if the file is deleted, FALSE otherwise.
     * @link http://php.net/unlink
     * @static
     */
    public static function deleteFile($f){
        $v = false;
        if($f instanceof RaiseFile){
            $v = $f->delete();
        }else{
            $v = unlink($f);
        }
        return (bool)$v;
    }

    /**
     * Create a directory path recursively
     * @param string $f The directory path to create
     * @param integer $perm (optional) Permissions of the directory path. Defaults to 0777
     * @return boolean TRUE if the creation was successful, FALSE otherwise.
     * @link http://php.net/mkdir
     * @static
     */
    public static function createPath($f, $perm = 0777){
        return (bool)mkdir($f, $perm, true);
    }

    /**
     * Remove a particular directory
     * @param string $f The directory to delete
     * @return boolean TRUE if the directory is deleted, FALSE otherwise.
     * @link http://php.net/rmdir
     * @static
     */
    public static function deletePath($f){
        return (bool)rmdir($f);
    }

    /**
     * Empty the entire folder
     * @param string $p Directory path to empty its content
     */
    public static function emptyPath($p){
        $d = dir($p);
        while (($entry = $d->read()) !== false) {
            $tp = RaisePath::combine($d->path, '/' . $entry);
            $isdir = is_dir($tp);
            if($entry == '.' || $entry == '..'){
                
            }else{
                if($isdir){
                    self::emptyPath($tp);
                    rmdir($tp);
                }else{
                    unlink($tp);
                }
            }
        }
        $d->close();
    }

    /**
     * Get the available disk space of a specific directory
     * @param string $d The directory path
     * @return double
     * @link http://php.net/disk-free-space
     * @static
     */
    public static function diskFreeSpace($d){
        return disk_free_space($d);
    }

    /**
     * Get the total disk space of a specific directory
     * @param string $d The directory path
     * @return double
     * @link http://php.net/disk-total-space
     * @static
     */
    public static function diskTotalSpace($d){
        return disk_total_space($d);
    }

    /**
     * Search for files given a particular pattern
     * @param string $pattern Pattern to use for search. See link for more info.
     * @param integer $flags (optional) Flags to use for the pattern search. See link for more info.
     * @return RaiseCollection The collection of files matching the pattern.
     * @link http://php.net/glob
     * @static
     */
    public static function pathSearch($pattern, $flags = 0){
        $a = glob($pattern, $flags);
        if($a === false){
            throw new RaiseException('Exception undefined. ', 0);
        }
        return new RaiseCollection($a);
    }
    
}
